<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src='https://cdn.staticfile.org/jquery/3.6.4/jquery.min.js'></script>
</head>
<body>
    <button id='btnGet'>Get</button>
    <button id='btnPost'>Post</button>
    <button id='btnDelete'>Delete</button>
    <button id='btnJsonp'>Jsonp</button>

    <script>
        // 这是jQuery语法，打开html页面是基于文本协议打开的，ajax中的url是基于http协议请求的，所以会出现跨域问题
        $(() => {
            // 处理get请求
            $('#btnGet').on('click', () => {    //注意选择id选择器的时候要用#
                $.ajax({
                    type: 'GET',
                    url: 'http://127.0.0.1/api/get',
                    data: {name: 'get', age: 20},
                    success: (res) => {
                        console.log(res)
                    }
                })
            })

            // 处理post请求
            $('#btnPost').on('click', () => {
                $.ajax({
                    type: 'POST',
                    url: 'http://127.0.0.1/api/post',
                    data: {name: 'post', age: 21},
                    success: (res) => {
                        console.log(res)
                    }
                })
            })

            // 处理delete请求
            $('#btnDelete').on('click', () => {
                $.ajax({
                    type: 'DELETE',
                    url: 'http:/127.0.0.1/api/delete',
                    data: {name: 'delete', age: 22},
                    success: (res) => {
                        console.log(res)
                    }
                })
            })

            let jsonp_func = (data) => {
                data.name = 'jsonp_func'

                console.log(data)
            }

            // 处理Jsonp请求
            $('#btnJsonp').on('click', () => {
                $.ajax({
                    type: 'GET',
                    url: `http://127.0.0.1/api/jsonp`,
                    dataType: 'jsonp',
                    success: (res) => {
                        console.log(res)
                    }
                })
            })
        })
    </script>
</body>
</html>